<?php
/** MY_html_helper
	* 
	* @package Car Club
	* @subpackage Helpers
	* @category HTML
	* @author Ray Nowell
	* 
	* This helper is designed to provided a central location for commonly used html sections of code.
	* Primarily created this to provide a common location for all grid javascript functions
	*/  


	function get_grid_doc_ready_javascript($use_copy = false)
	{
			$ret_val = 'getGridDocReady();';
		return $ret_val;
	}
	
	
	/**
	* Returns the Table Template used for all grids
	* 
	* @access public 
	* @return string */ 
	function set_table_template()
	{
		$tmpl = array (
										'table_open'          => '<table class="grid" border="0" cellpadding="4" cellspacing="0">',

										'heading_row_start'   => '<tr class="gridHeader">',
										'heading_row_end'     => '</tr>',
										'heading_cell_start'  => '<th>',
										'heading_cell_end'    => '</th>',

										'row_start'           => '<tr class="gridRow">',
										'row_end'             => '</tr>',
										'cell_start'          => '<td>',
										'cell_end'            => '</td>',

										'row_alt_start'       => '<tr class="gridAlternatingRow">',
										'row_alt_end'         => '</tr>',
										'cell_alt_start'      => '<td>',
										'cell_alt_end'        => '</td>',

										'table_close'         => '</table>'
									);
									
		return $tmpl;
	}
	
	/** 
	 * Takes an array_result and turns it in an array
	 * @access public 
	 * @return */
	function set_lookup($lookup_array,$field_name_key,$field_name_value,$add_empty_value = false)
	{
		$return_array = array();

		if ($add_empty_value)
			$return_array[-1] = '';

		foreach ($lookup_array as $row)
			$return_array[$row[$field_name_key]] = $row[$field_name_value]; 

		return $return_array;
	}
	
	function format_percent($passed_number)
	{
		$ret_val = (round($passed_number * 1000))/10;
		return $ret_val.'%';
	}
	
	//http://smashscripts.blogspot.com/2008/07/php-format-currency-function.html
	//constants defined in constants.php
	function format_currency($amount, $NumDigitsAfterDecimal = 2, $IncludeLeadingDigit = false, $UseParensForNegativeNumbers = true, $GroupDigits = true)
	{
	
		// export the values returned by localeconv into the local scope
		if (function_exists("localeconv")) extract(localeconv());
		
		// set defaults if locale is not set
		if (empty($currency_symbol)) $currency_symbol = DEFAULT_CURRENCY_SYMBOL;
		if (empty($mon_decimal_point)) $mon_decimal_point = DEFAULT_MON_DECIMAL_POINT;
		if (empty($mon_thousands_sep)) $mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
		if (empty($positive_sign)) $positive_sign = DEFAULT_POSITIVE_SIGN;
		if (empty($negative_sign)) $negative_sign = DEFAULT_NEGATIVE_SIGN;
		if (empty($frac_digits) || $frac_digits == CHAR_MAX) $frac_digits = DEFAULT_FRAC_DIGITS;
		if (empty($p_cs_precedes) || $p_cs_precedes == CHAR_MAX) $p_cs_precedes = DEFAULT_P_CS_PRECEDES;
		if (empty($p_sep_by_space) || $p_sep_by_space == CHAR_MAX) $p_sep_by_space = DEFAULT_P_SEP_BY_SPACE;
		if (empty($n_cs_precedes) || $n_cs_precedes == CHAR_MAX) $n_cs_precedes = DEFAULT_N_CS_PRECEDES;
		if (empty($n_sep_by_space) || $n_sep_by_space == CHAR_MAX) $n_sep_by_space = DEFAULT_N_SEP_BY_SPACE;
		if (empty($p_sign_posn) || $p_sign_posn == CHAR_MAX) $p_sign_posn = DEFAULT_P_SIGN_POSN;
		if (empty($n_sign_posn) || $n_sign_posn == CHAR_MAX) $n_sign_posn = DEFAULT_N_SIGN_POSN;
		
		// check $NumDigitsAfterDecimal
		if ($NumDigitsAfterDecimal > -1)
		$frac_digits = $NumDigitsAfterDecimal;
		
		// check $UseParensForNegativeNumbers
		if ($UseParensForNegativeNumbers == -1) {
		$n_sign_posn = 0;
		if ($p_sign_posn == 0) {
		if (DEFAULT_P_SIGN_POSN != 0)
		$p_sign_posn = DEFAULT_P_SIGN_POSN;
		else
		$p_sign_posn = 3;
		}
		} elseif ($UseParensForNegativeNumbers == 0) {
		if ($n_sign_posn == 0)
		if (DEFAULT_P_SIGN_POSN != 0)
		$n_sign_posn = DEFAULT_P_SIGN_POSN;
		else
		$n_sign_posn = 3;
		}
		
		// check $GroupDigits
		if ($GroupDigits == -1) {
		$mon_thousands_sep = DEFAULT_MON_THOUSANDS_SEP;
		} elseif ($GroupDigits == 0) {
		$mon_thousands_sep = "";
		}
		
		// start by formatting the unsigned number
		$number = number_format(abs($amount),
		$frac_digits,
		$mon_decimal_point,
		$mon_thousands_sep);
		
		// check $IncludeLeadingDigit
		if ($IncludeLeadingDigit == 0) {
		if (substr($number, 0, 2) == "0.")
		$number = substr($number, 1, strlen($number)-1);
		}
		if ($amount < 0) {
		$sign = $negative_sign;
		
		// "extracts" the boolean value as an integer
		$n_cs_precedes = intval($n_cs_precedes == true);
		$n_sep_by_space = intval($n_sep_by_space == true);
		$key = $n_cs_precedes . $n_sep_by_space . $n_sign_posn;
		} else {
		$sign = $positive_sign;
		$p_cs_precedes = intval($p_cs_precedes == true);
		$p_sep_by_space = intval($p_sep_by_space == true);
		$key = $p_cs_precedes . $p_sep_by_space . $p_sign_posn;
		}
		$formats = array(
		
		// currency symbol is after amount
		
		// no space between amount and sign
		'000' => '(%s' . $currency_symbol . ')',
		'001' => $sign . '%s ' . $currency_symbol,
		'002' => '%s' . $currency_symbol . $sign,
		'003' => '%s' . $sign . $currency_symbol,
		'004' => '%s' . $sign . $currency_symbol,
		
		// one space between amount and sign
		'010' => '(%s ' . $currency_symbol . ')',
		'011' => $sign . '%s ' . $currency_symbol,
		'012' => '%s ' . $currency_symbol . $sign,
		'013' => '%s ' . $sign . $currency_symbol,
		'014' => '%s ' . $sign . $currency_symbol,
		
		// currency symbol is before amount
		
		// no space between amount and sign
		'100' => '(' . $currency_symbol . '%s)',
		'101' => $sign . $currency_symbol . '%s',
		'102' => $currency_symbol . '%s' . $sign,
		'103' => $sign . $currency_symbol . '%s',
		'104' => $currency_symbol . $sign . '%s',
		
		// one space between amount and sign
		'110' => '(' . $currency_symbol . ' %s)',
		'111' => $sign . $currency_symbol . ' %s',
		'112' => $currency_symbol . ' %s' . $sign,
		'113' => $sign . $currency_symbol . ' %s',
		'114' => $currency_symbol . ' ' . $sign . '%s');
		
		// lookup the key in the above array
		return sprintf($formats[$key], $number);
	}
	
	
/* End of file MY_html_helper.php */
/* Location: ./system/application/helpers/MY_html_helper.php */
